package com.example.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.config.ResponseBean;
import com.example.config.UnicomResponseEnums;
import com.example.sys.entity.Config;
import com.example.sys.entity.DictData;
import com.example.sys.service.ConfigService;
import com.example.sys.vo.ConfigVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 参数配置表 前端控制器
 * </p>
 *
 * @author anan
 * @since 2019-06-20
 */
@RestController
@RequestMapping("/sys/config")
public class ConfigController {

    @Autowired
    private ConfigService configService;

    @GetMapping("/getConfigList")
    public ResponseBean<UnicomResponseEnums> getConfigList(ConfigVo configVo){
        System.out.println(configVo);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(configVo, false, true);
        if (stringObjectMap.isEmpty()){
            List<Config> configs = configService.list();
            return new ResponseBean(true, configs, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true, configService.selectPage(configVo), UnicomResponseEnums.SUCCESS_OPTION);
    }

    @DeleteMapping("/deleteByConfigId")
    public ResponseBean<UnicomResponseEnums> deleteByConfigId(@RequestBody ConfigVo configVo){
        if (configVo.getConfigId()!=null && configService.removeById(configVo.getConfigId())){
            return new ResponseBean(true,UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true, UnicomResponseEnums.OPERATION_FAILED);
    }

    @PostMapping("/updateConfigById")
    public ResponseBean<UnicomResponseEnums> updateConfigById(@RequestBody @Valid Config config){
        if (config.getConfigId()!=null && configService.updateById(config)){
            return new ResponseBean(true,UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true, UnicomResponseEnums.OPERATION_FAILED);
    }

    @PostMapping("/addConfig")
    public ResponseBean<UnicomResponseEnums> addConfig(@RequestBody @Valid Config config){
        if (!config.toString().isEmpty()) {
            configService.save(config);
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }
}
